/*
 * sys.c
 *
 *  Created on: 28 Dec 2012
 *      Author: adan
 */

#include "sys.h"

/**
 * creates an object, ensures the memory is blank
 * TODO register the object with the object control system
 */
void* createObject(size_t size) {
	void* obj = malloc(size);
	memset(obj, 0, size);
	return obj;
}

/**
 * SDL_gfx takes 0xRRGGBBAA, but the SDL surface is not necessarily in this format!
 */
Uint32 convertToRGBA(Colour colour) {
	return ((colour&pixelFormat->Rmask)>>pixelFormat->Rshift) << 24 |
			((colour&pixelFormat->Gmask)>>pixelFormat->Gshift) << 16 |
			((colour&pixelFormat->Bmask)>>pixelFormat->Bshift) << 8 |
			0xFF;
}

Uint32 convertToRGBABrightness(Colour colour, int brightenBy) {
	return (((colour&pixelFormat->Rmask)>>pixelFormat->Rshift)+brightenBy) << 24 |
			(((colour&pixelFormat->Gmask)>>pixelFormat->Gshift)+brightenBy) << 16 |
			(((colour&pixelFormat->Bmask)>>pixelFormat->Bshift)+brightenBy) << 8 |
			0xFF;
}

bool hasCollided(SDL_Rect* r1, SDL_Rect* r2) {
        if (
                        (r1->x > r2->x + r2->w) ||
                        (r1->y > r2->y + r2->h) ||
                        (r2->x > r1->x + r1->w) ||
                        (r2->y > r1->y + r1->h)
        ) {
                return FALSE;
        }

        return TRUE;
}

SDL_Rect* rect(int x, int y, int w, int h) {
	SDL_Rect* rect = (SDL_Rect*)malloc(sizeof(SDL_Rect));
	rect->w = w;
	rect->h = h;
	rect->x = x;
	rect->y = y;
	return rect;
}

